package com.nononsenseapps.feeder.ui.feed

import android.text.Spannable
import android.text.SpannableString
import android.text.style.TextAppearanceSpan
import android.view.View
import com.nononsenseapps.feeder.ui.R
import com.nononsenseapps.feeder.ui.common.RoundedCornerImageView

class FeedItemHolder_Card(view: View, actionCallback: ActionCallback,viewModel:Boolean) :
        FeedItemHolder_Mini(view,actionCallback,viewModel){
    init {
        imageView.radius= RoundedCornerImageView.Radius(imageView.dipsToPix(8F))
    }

    override fun initListener() {
        super.initListener()
        imageView.isClickable=false
        imageView.isLongClickable=false
    }

    override fun fillTitle(forceRead: Boolean) {
        titleTextView.visibility = View.VISIBLE
        rssItem?.let { rssItem ->
            // \u2014 is a EM-dash, basically a long version of '-'
            val temps = if (rssItem.plainSnippet.isEmpty())
                rssItem.plainTitle
            else
                rssItem.plainTitle + " \n.\t\t" + rssItem.plainSnippet + "\u2026"
            val textSpan = SpannableString(temps)

            textSpan.setSpan(
                TextAppearanceSpan(view.context, R.style.TextAppearance_ListItem_Body),
                rssItem.plainTitle.length, temps.length,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

            if (rssItem.unread && !forceRead) {
                textSpan.setSpan(
                    TextAppearanceSpan(view.context, R.style.TextAppearance_ListItem_Title),
                    0, rssItem.plainTitle.length,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            } else {
                textSpan.setSpan(
                    TextAppearanceSpan(view.context, R.style.TextAppearance_ListItem_Title_Read),
                    0, rssItem.plainTitle.length,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            }
            titleTextView.text = textSpan
        }
    }
}